﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Common.Logging;
using In.Api.Dto.HisWs;
using In.Api.Helpers;
using InDbContext;
using Newtonsoft.Json;
using Oracle.DataAccess.Client;

namespace In.Api.Controllers
{
    public class HisSpController : ApiController
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(HisSpController));


        #region 获取科室信息
        /// <summary>
        /// 获取科室信息        
        /// </summary>
        [HttpPost]
        [ActionName("GetDeptInfo")]
        public GetDeptInfoResponse GetDeptInfo(GetDeptInfoRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("hospitalId", OracleDbType.Varchar2, 100),
                new OracleParameter("branchNo", OracleDbType.Varchar2, 100), //注意：这里参数名称前面都不能加":"
                new OracleParameter("deptId", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor)
            };
            parameter[0].Value = "0";
            parameter[1].Value = "0";
            parameter[2].Value = request.DeptId;
            parameter[3].Direction = System.Data.ParameterDirection.Output;
            parameter[4].Direction = System.Data.ParameterDirection.Output;
            parameter[5].Direction = System.Data.ParameterDirection.Output;
            DataTable dt;
            try
            {
                dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetDeptInfo", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetDeptInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetDeptInfoResponse();
            var resultCode = parameter[3].Value.ToString();
            var resultDesc = parameter[4].Value.ToString();

            HospSpLogHelper.Log("GetDeptInfo",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.DeptList = new List<GetDeptInfoResponse.DeptInfo>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var deptObj = new GetDeptInfoResponse.DeptInfo();
                var deptName = dt.Rows[i]["deptName"].ToString();
                var deptId = dt.Rows[i]["deptId"].ToString();
                //var apptDapt = dt.Rows[i]["apptDapt"].ToString();
                var loc = dt.Rows[i]["position"].ToString();
                var parentId = dt.Rows[i]["parentId"].ToString();
                var desc = dt.Rows[i]["deptsc"].ToString();

                deptObj.DeptId = deptId;
                deptObj.DeptName = deptName;
                deptObj.Loc = loc;
                // deptObj.ApptDapt = apptDapt;
                deptObj.ParentId = parentId;
                deptObj.Desc = desc;

                response.DeptList.Add(deptObj);
            }

            return response;
        }
        #endregion

        #region 获取医生信息
        /// <summary>
        ///  获取医生信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("GetDoctorInfo")]
        public GetDoctorInfoResponse GetDoctorInfo(GetDoctorInfoRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("deptId", OracleDbType.Varchar2, 100),
                new OracleParameter("doctorId", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor)
            };
            parameter[0].Value = request.DeptId;
            parameter[1].Value = request.DoctorId;
            parameter[2].Direction = System.Data.ParameterDirection.Output;
            parameter[3].Direction = System.Data.ParameterDirection.Output;
            parameter[4].Direction = System.Data.ParameterDirection.Output;
            DataTable dt;
            try
            {
                dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetDoctorInfo", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetDoctorInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetDoctorInfoResponse();
            var resultCode = parameter[2].Value.ToString();
            var resultDesc = parameter[3].Value.ToString();

            HospSpLogHelper.Log("GetDoctorInfo",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.DoctorList = new List<GetDoctorInfoResponse.DoctorInfo>();
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var doctorInfoObj = new GetDoctorInfoResponse.DoctorInfo();
                var doctorId = dt.Rows[i]["doctorId"].ToString();
                var doctorName = dt.Rows[i]["doctorName"].ToString();
                var deptId = dt.Rows[i]["deptId"].ToString();
                var title = dt.Rows[i]["title"].ToString();
                var fee = "";
                var gender = dt.Rows[i]["gender"].ToString();
                var desc = dt.Rows[i]["drdesc"].ToString();

                doctorInfoObj.DoctorId = doctorId;
                doctorInfoObj.DoctorName = doctorName;
                doctorInfoObj.DeptId = deptId;
                doctorInfoObj.Title = title;
                doctorInfoObj.Fee = fee;
                doctorInfoObj.Gender = gender;
                doctorInfoObj.Desc = desc;

                response.DoctorList.Add(doctorInfoObj);
            }

            return response;
        }
        #endregion

        #region 患者建卡
        /// <summary>
        ///  建卡
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("CreateCard")]
        public CreateCardResponse CreateCard(CreateCardRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("idenNo", OracleDbType.Varchar2, 100),
                new OracleParameter("name", OracleDbType.Varchar2, 100),
                new OracleParameter("gender", OracleDbType.Varchar2, 100),
                new OracleParameter("birthday", OracleDbType.Varchar2, 100),
                new OracleParameter("mobile", OracleDbType.Varchar2, 100),
                new OracleParameter("address", OracleDbType.Varchar2, 100),
                new OracleParameter("cardno", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor)
            };
            parameter[0].Value = request.IdenNo;
            parameter[1].Value = request.Name;
            parameter[2].Value = request.Gender;
            parameter[3].Value = request.Birthdate;
            parameter[4].Value = request.Phone;
            parameter[5].Value = request.Address;
            parameter[6].Value = request.CardNo;
            parameter[7].Direction = System.Data.ParameterDirection.Output;
            parameter[8].Direction = System.Data.ParameterDirection.Output;
            parameter[9].Direction = System.Data.ParameterDirection.Output;
            DataTable dt;
            try
            {
                var ds = OracleDataAccessHelper.RunProcedure("BKCloud_CreateCard", parameter, "ds");
                Logger.InfoFormat("0", JsonConvert.SerializeObject(ds));
                if (ds.Tables.Count > 0)
                    dt = ds.Tables[0];
                else
                {
                    dt = null;
                }
                // dt = OracleDataAccessHelper.RunProcedure("BKCloud_CreateCard", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_CreateCard 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new CreateCardResponse();
            var resultCode = parameter[7].Value.ToString();
            var resultDesc = parameter[8].Value.ToString();

            HospSpLogHelper.Log("CreateCard",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.Card = new CreateCardResponse.CreateCard();


            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var createCardObj = new CreateCardResponse.CreateCard();
                var cardType = dt.Rows[i]["cardType"].ToString() ?? "";
                var cardNo = dt.Rows[i]["cardNo"].ToString() ?? "";
                var hisDate = dt.Rows[i]["hisDate"].ToString() ?? "";

                createCardObj.CardNo = cardNo;
                createCardObj.CardType = cardType;
                createCardObj.HisDate = hisDate;

                response.Card = createCardObj;
            }

            return response;
        }
        #endregion

        #region 获取患者信息
        /// <summary>
        /// 获取患者信息
        /// </summary>
        [HttpPost]
        [ActionName("GetPatientInfo")]
        public GetPatientInfoResponse GetPatientInfo(GetPatientInfoRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("cardNo", OracleDbType.Varchar2, 100),
                new OracleParameter("cardType", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor)
            };
            parameter[0].Value = request.CardNo;
            parameter[1].Value = GetCardTypeName(request.CardType);
            parameter[2].Direction = System.Data.ParameterDirection.Output;
            parameter[3].Direction = System.Data.ParameterDirection.Output;
            parameter[4].Direction = System.Data.ParameterDirection.Output;
            DataTable dt;
            try
            {
                dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetpatientInfo", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetpatientInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetPatientInfoResponse();
            var resultCode = parameter[2].Value.ToString();
            var resultDesc = parameter[3].Value.ToString();

            HospSpLogHelper.Log("GetPatientInfo",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.PatientInfo = new GetPatientInfoResponse.Patient();
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var patientInfoObj = new GetPatientInfoResponse.Patient();
                var patientId = dt.Rows[i]["patientId"].ToString();
                var outpatientNo = dt.Rows[i]["outpatientNo"].ToString();
                var inpatientNo = dt.Rows[i]["inpatientNo"].ToString();
                var name = dt.Rows[i]["patientName"].ToString();
                var gender = dt.Rows[i]["gender"].ToString();
                var age = dt.Rows[i]["age"].ToString();
                var birthDate = dt.Rows[i]["birthday"].ToString();
                var idenNo = dt.Rows[i]["idenNo"].ToString();
                var identity = dt.Rows[i]["identity"].ToString();
                var phone = dt.Rows[i]["phone"].ToString();
                var address = dt.Rows[i]["address"].ToString();
                var feeType = "";//dt.Rows[i]["feeType"].ToString();

                patientInfoObj.PatientId = patientId;
                patientInfoObj.OutpatientNo = outpatientNo;
                patientInfoObj.InpatientNo = inpatientNo;
                patientInfoObj.Name = name;
                patientInfoObj.Gender = gender;
                patientInfoObj.Age = age;
                patientInfoObj.Gender = gender;
                patientInfoObj.Birthdate = birthDate;
                patientInfoObj.IdenNo = idenNo;
                patientInfoObj.Identity = identity;
                patientInfoObj.Phone = phone;
                patientInfoObj.FeeType = feeType;

                response.PatientInfo = patientInfoObj;

                //在前置机HospDatabase数据库HospCard加入患者就诊卡，用于在通知功能筛选患者
                //using (var ctx = new HospDatabaseEntity())
                //{
                //    var patient = ctx.HospCards.FirstOrDefault(card => card.PatientId == patientId);
                //    if (patient.IsNull())
                //    {
                //        var card = new HospCard();
                //        card.PatientId = patientId;
                //        card.Name = name;
                //        card.CardNo = request.CardNo;
                //        card.CardType = request.CardType;
                //        ctx.HospCards.Add(card);
                //        ctx.SaveChanges();
                //    }
                //}
            }

            return response;
        }
        #endregion

        #region 获取出诊科室列表（无用）
        /// <summary>
        /// 获取出诊科室列表        
        /// </summary>
        //[HttpPost]
        //[ActionName("GetDeptList")]
        //public GetDeptListResponse GetDeptList(GetDeptListRequest request)
        //{
        //    OracleParameter[] parameter =
        //    {
        //        new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
        //        new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
        //        new OracleParameter("data", OracleDbType.RefCursor)
        //    };

        //    parameter[0].Direction = System.Data.ParameterDirection.Output;
        //    parameter[1].Direction = System.Data.ParameterDirection.Output;
        //    parameter[2].Direction = System.Data.ParameterDirection.Output;
        //    DataTable dt;

        //    try
        //    {
        //        dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetDeptList", parameter, "ds").Tables[0];
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.ErrorFormat("调用 BKCloud_GetDeptList 失败，信息：{0}", JsonConvert.SerializeObject(ex));

        //        throw;
        //    }

        //    var response = new GetDeptListResponse();
        //    var resultCode = parameter[0].Value.ToString();
        //    var resultDesc = parameter[1].Value.ToString();

        //    HospSpLogHelper.Log("GetDeptList",
        //        JsonConvert.SerializeObject(request),
        //        JsonConvert.SerializeObject(new
        //        {
        //            ResultCode = resultCode,
        //            ResultDesc = resultDesc,
        //            Data = dt
        //        }));

        //    if (resultCode != "0")
        //    {
        //        response.ResultCode = resultCode;
        //        response.ResultDesc = resultDesc;
        //        return response;
        //    }

        //    response.ResultCode = resultCode;
        //    response.ResultDesc = resultDesc;
        //    response.DeptList = new List<GetDeptListResponse.Dept>();
        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {
        //        var deptObj = new GetDeptListResponse.Dept();
        //        var deptName = dt.Rows[i]["deptName"].ToString();
        //        var deptId = dt.Rows[i]["deptId"].ToString();

        //        deptObj.DeptId = deptId;
        //        deptObj.DeptName = deptName;
        //        response.DeptList.Add(deptObj);
        //    }

        //    return response;
        //}


        /// <summary>
        /// 获取出诊科室列表（仅限获取有排班的科室）
        /// </summary>
        //[HttpPost]
        //[ActionName("GetDeptSchedule")]
        //public GetScheduleInfoResponse GetDeptSchedule(GetScheduleInfoRequest request)
        //{
        //    OracleParameter[] parameter =
        //    {
        //        new OracleParameter("regType", OracleDbType.Varchar2, 100),
        //        new OracleParameter("regDate", OracleDbType.Varchar2, 100),
        //        new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
        //        new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
        //        new OracleParameter("cx_cursor", OracleDbType.RefCursor)
        //    };
        //    parameter[0].Value = request.RegType;
        //    parameter[1].Value = request.RegDate;
        //    parameter[2].Value = System.Data.ParameterDirection.Output;
        //    parameter[3].Direction = System.Data.ParameterDirection.Output;
        //    parameter[4].Direction = System.Data.ParameterDirection.Output;
        //    DataTable dt;
        //    try
        //    {
        //        dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetDeptSchedule", parameter, "ds").Tables[0];
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.ErrorFormat("调用 BKCloud_GetDeptSchedule 失败，信息：{0}", JsonConvert.SerializeObject(ex));

        //        throw;
        //    }

        //    var response = new GetScheduleInfoResponse();
        //    var resultCode = parameter[2].Value.ToString();
        //    var resultDesc = parameter[3].Value.ToString();

        //    HospSpLogHelper.Log("GetDeptSchedule",
        //        JsonConvert.SerializeObject(request),
        //        JsonConvert.SerializeObject(new
        //        {
        //            ResultCode = resultCode,
        //            ResultDesc = resultDesc,
        //            Data = dt
        //        }));

        //    if (resultCode != "0")
        //    {
        //        response.ResultCode = resultCode;
        //        response.ResultDesc = resultDesc;
        //        return response;
        //    }

        //    response.ResultCode = resultCode;
        //    response.ResultDesc = resultDesc;
        //    response.ScheduleList = new List<GetScheduleInfoResponse.ScheduleInfo>();

        //    for (var i = 0; i < dt.Rows.Count; i++)
        //    {
        //        var deptScheduleInfoObj = new GetScheduleInfoResponse.ScheduleInfo();
        //        var deptId = dt.Rows[i]["deptId"].ToString();
        //        var deptName = dt.Rows[i]["deptName"].ToString();
        //        var deptLocation = dt.Rows[i]["deptLocation"].ToString();
        //        var deptDoctors = dt.Rows[i]["deptDoctors"].ToString();
        //        var canAppoint = dt.Rows[i]["canAppoint"].ToString();

        //        deptScheduleInfoObj.DeptId = deptId;
        //        deptScheduleInfoObj.DeptName = deptName;
        //        deptScheduleInfoObj.DeptLocation = deptLocation;
        //        deptScheduleInfoObj.DeptDoctors = deptDoctors;
        //        deptScheduleInfoObj.CanAppoint = canAppoint;

        //        response.DeptScheduleList.Add(deptScheduleInfoObj);
        //    }

        //    return response;
        //}

        #endregion

        #region 获取排班信息
        /// <summary>
        /// 获取排班信息
        /// </summary>
        [HttpPost]
        [ActionName("GetScheduleInfo")]
        public GetScheduleInfoResponse GetScheduleInfo(GetScheduleInfoRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("hospitalId", OracleDbType.Varchar2, 100),
                new OracleParameter("deptId", OracleDbType.Varchar2, 100),
                new OracleParameter("doctorId", OracleDbType.Varchar2, 100),
                //new OracleParameter("beginDate", OracleDbType.Varchar2, 100),
                //new OracleParameter("endDate", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor)
            };
            parameter[0].Value = "0";
            parameter[1].Value = request.DeptId;
            parameter[2].Value = request.DoctorId;
            //parameter[2].Value = request.BeginDate;
            //parameter[3].Value = request.EndDate;
            parameter[3].Direction = System.Data.ParameterDirection.Output;
            parameter[4].Direction = System.Data.ParameterDirection.Output;
            parameter[5].Direction = System.Data.ParameterDirection.Output;
            DataTable dt;
            try
            {
                var ds = OracleDataAccessHelper.RunProcedure("BKCloud_GetScheduleInfo", parameter, "ds");
                Logger.InfoFormat("0", JsonConvert.SerializeObject(ds));
                if (ds.Tables.Count > 0)
                    dt = ds.Tables[0];
                else
                {
                    dt = null;
                }
                // dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetScheduleInfo", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetScheduleInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));
                throw;
            }

            var response = new GetScheduleInfoResponse();
            var resultCode = parameter[3].Value.ToString();
            var resultDesc = parameter[4].Value.ToString();

            HospSpLogHelper.Log("GetScheduleInfo",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.ScheduleList = new List<GetScheduleInfoResponse.ScheduleInfo>();

            //string weekstr = DateTime.Now.DayOfWeek.ToString();
            //switch (weekstr)
            //{
            //    case "Monday": weekstr = "星期一"; break;
            //    case "Tuesday": weekstr = "星期二"; break;
            //    case "Wednesday": weekstr = "星期三"; break;
            //    case "Thursday": weekstr = "星期四"; break;
            //    case "Friday": weekstr = "星期五"; break;
            //    case "Saturday": weekstr = "星期六"; break;
            //    case "Sunday": weekstr = "星期日"; break;
            //}
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var deptId = dt.Rows[i]["deptId"].ToString();
                var deptName = dt.Rows[i]["deptName"].ToString();
                var regType = dt.Rows[i]["regType"].ToString();
                var doctorId = dt.Rows[i]["doctorId"].ToString();
                var doctorName = dt.Rows[i]["doctorname"].ToString();
                var timeSlice = dt.Rows[i]["timeSlice"].ToString();
                var regDate = dt.Rows[i]["regDate"].ToString();
                var isHalt = "0";//dt.Rows[i]["isHalt"].ToString();
                var isTimeFlag = dt.Rows[i]["isTimeFlag"].ToString();
                var fee = dt.Rows[i]["fee"].ToString();
                var treatFee = dt.Rows[i]["treatFee"].ToString();
                var otherFee = "0";//dt.Rows[i]["otherFee"].ToString();
                // 已经结束的排班不显示
                //if (string.CompareOrdinal(regDate, DateTime.Now.ToString("yyyy-MM-dd")) < 0)
                //{
                //    continue;
                //}

                var scheduleObj = new GetScheduleInfoResponse.ScheduleInfo();
                // scheduleObj.Scheduleld = scheduleld;
                scheduleObj.DeptId = deptId;
                scheduleObj.DeptName = deptName;
                scheduleObj.DoctorId = doctorId;
                scheduleObj.DoctorName = doctorName;
                scheduleObj.RegType = regType;
                // scheduleObj.SubjectName = subjectName;
                scheduleObj.RegDate = regDate;
                scheduleObj.IsHalt = isHalt;
                scheduleObj.TimeSlice = timeSlice;
                scheduleObj.IsTimeFlag = isTimeFlag;
                scheduleObj.Fee = fee;
                scheduleObj.TreatFee = treatFee;
                scheduleObj.OtherFee = otherFee;

                response.ScheduleList.Add(scheduleObj);
            }

            return response;
        }
        #endregion

        #region 获取排班分时信息
        /// <summary>
        /// 获取排班分时信息
        /// </summary>
        [HttpPost]
        [ActionName("GetTimeRegInfo")]
        public GetTimeRegInfoResponse GetTimeRegInfo(GetTimeRegInfoRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("hospitalId", OracleDbType.Varchar2, 100),
                new OracleParameter("deptId", OracleDbType.Varchar2, 100),
                new OracleParameter("doctorId", OracleDbType.Varchar2, 100),
                new OracleParameter("regDate", OracleDbType.Varchar2, 100),
                new OracleParameter("timeSlice", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor)
            };
            parameter[0].Value = "0";
            parameter[1].Value = request.DeptId;
            parameter[2].Value = request.DoctorId;
            parameter[3].Value = request.RegDate;
            parameter[4].Value = request.TimeSlice;
            parameter[5].Direction = System.Data.ParameterDirection.Output;
            parameter[6].Direction = System.Data.ParameterDirection.Output;
            parameter[7].Direction = System.Data.ParameterDirection.Output;
            DataTable dt;
            try
            {
                dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetTimeRegInfo", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetTimeRegInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetTimeRegInfoResponse();
            var resultCode = parameter[5].Value.ToString();
            var resultDesc = parameter[6].Value.ToString();

            HospSpLogHelper.Log("GetTimeRegInfo",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.TimeRegList = new List<GetTimeRegInfoResponse.TimeRegInfo>();
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var timeRegObj = new GetTimeRegInfoResponse.TimeRegInfo();
                var sourceCode = dt.Rows[i]["sourceCode"].ToString();
                var startTime = dt.Rows[i]["startTime"].ToString();
                var endTime = dt.Rows[i]["endTime"].ToString();
                var totalCount = dt.Rows[i]["totalcount"].ToString();
                var leaveCount = dt.Rows[i]["leavecount"].ToString();
                var sqNo = dt.Rows[i]["seqno"].ToString();

                timeRegObj.SourceCode = sourceCode;
                timeRegObj.StartTime = startTime;
                timeRegObj.EndTime = endTime;
                timeRegObj.TotalCount = totalCount;
                timeRegObj.LeaveCount = leaveCount;
                timeRegObj.SqNo = sqNo;

                response.TimeRegList.Add(timeRegObj);
            }

            return response;
        }
        #endregion

        #region 预约挂号
        /// <summary>
        /// 预约挂号
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("ExeRegister")]
        public ExeRegisterResponse ExeRegister(ExeRegisterRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("regFlag", OracleDbType.Varchar2, 100),
                new OracleParameter("payType", OracleDbType.Varchar2, 100),
                new OracleParameter("regDate", OracleDbType.Varchar2, 100),
                new OracleParameter("startTime", OracleDbType.Varchar2, 100),
                new OracleParameter("orderId", OracleDbType.Varchar2, 100),
                new OracleParameter("cardNo", OracleDbType.Varchar2, 100),
                new OracleParameter("cardType", OracleDbType.Varchar2, 100),
                new OracleParameter("transNo ", OracleDbType.Varchar2, 100),
                new OracleParameter("transTime", OracleDbType.Varchar2, 100),
                new OracleParameter("sourceCode", OracleDbType.Varchar2, 100),
                new OracleParameter("sqno", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("position", OracleDbType.Varchar2, 100),
                new OracleParameter("regNo", OracleDbType.Varchar2, 100),
                new OracleParameter("flowno", OracleDbType.Varchar2, 100),
                new OracleParameter("hisDate", OracleDbType.Varchar2, 100),
            };
            parameter[0].Value = request.RegFlag;
            parameter[1].Value = request.PayType;
            parameter[2].Value = request.RegDate;
            parameter[3].Value = request.StartTime;
            parameter[4].Value = request.OrderId;
            parameter[5].Value = request.CardNo;
            parameter[6].Value = request.CardType;
            parameter[7].Value = request.TransNo;
            parameter[8].Value = request.TransTime;
            parameter[9].Value = request.SourceCode;
            parameter[10].Value = request.Sqno;
            parameter[11].Direction = System.Data.ParameterDirection.Output;
            parameter[12].Direction = System.Data.ParameterDirection.Output;
            parameter[13].Direction = System.Data.ParameterDirection.Output;
            parameter[14].Direction = System.Data.ParameterDirection.Output;
            parameter[15].Direction = System.Data.ParameterDirection.Output;
            parameter[16].Direction = System.Data.ParameterDirection.Output;

            try
            {
                OracleDataAccessHelper.RunProcedure("BKCloud_ExeRegister", parameter, "ds");
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_ExeRegister 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new ExeRegisterResponse();
            var resultCode = parameter[11].Value.ToString();
            var resultDesc = parameter[12].Value.ToString();
            var position = parameter[13].Value.ToString();
            var regNo = parameter[14].Value.ToString();
            var flowNo = parameter[15].Value.ToString();
            var hisDate = parameter[16].Value.ToString();

            HospSpLogHelper.Log("ExeRegister",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Position = position,
                    RegNo = regNo,
                    FlowNo = flowNo,
                    HisDate = hisDate
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.Address = position;
            response.RegNo = regNo;
            response.VisitNo = flowNo;
            response.HisDate = hisDate;

            return response;
        }

        #endregion

        #region 挂号单状态查询
        /// <summary>
        /// 挂号单状态查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("GetOrderStatus")]
        public GetOrderStatusResponse GetOrderStatus(GetOrderStatusRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("flowno", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("status", OracleDbType.Varchar2, 100),
                new OracleParameter("visitNo", OracleDbType.Varchar2, 100),
                new OracleParameter("regNo", OracleDbType.Varchar2, 100),
                new OracleParameter("regDate", OracleDbType.Varchar2, 100),
                new OracleParameter("timeSlice", OracleDbType.Varchar2, 100),
                new OracleParameter("startTime", OracleDbType.Varchar2, 100),
                new OracleParameter("endTime", OracleDbType.Varchar2, 100),
                new OracleParameter("address", OracleDbType.Varchar2, 100),

            };
            parameter[0].Value = request.OrderId;
            parameter[1].Direction = System.Data.ParameterDirection.Output;
            parameter[2].Direction = System.Data.ParameterDirection.Output;
            parameter[3].Direction = System.Data.ParameterDirection.Output;
            parameter[4].Direction = System.Data.ParameterDirection.Output;
            parameter[5].Direction = System.Data.ParameterDirection.Output;
            parameter[6].Direction = System.Data.ParameterDirection.Output;
            parameter[7].Direction = System.Data.ParameterDirection.Output;
            parameter[8].Direction = System.Data.ParameterDirection.Output;
            parameter[9].Direction = System.Data.ParameterDirection.Output;
            parameter[10].Direction = System.Data.ParameterDirection.Output;

            try
            {
                OracleDataAccessHelper.RunProcedure("BKCloud_GetOrderStatus", parameter, "ds");
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetOrderStatus 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetOrderStatusResponse();
            var resultCode = parameter[1].Value.ToString();
            var resultDesc = parameter[2].Value.ToString();
            var status = parameter[3].Value.ToString();
            var visitNo = parameter[4].Value.ToString();
            var regNo = parameter[5].Value.ToString();
            var regDate = parameter[6].Value.ToString();
            var timeSlice = parameter[7].Value.ToString();
            var startTime = parameter[8].Value.ToString();
            var endTime = parameter[9].Value.ToString();
            var address = parameter[10].Value.ToString();


            HospSpLogHelper.Log("GetOrderStatus",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Status = status,
                    VisitNo = visitNo,
                    RegNo = regNo,
                    RegDate = regDate,
                    TimeSlice = timeSlice,
                    StartTime = startTime,
                    EndTime = endTime,
                    Address = address,

                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;

            response.Status = status;
            response.VisitNo = visitNo;
            response.RegNo = regNo;
            response.RegDate = regDate;
            response.TimeSlice = timeSlice;
            response.StartTime = startTime;
            response.EndTime = endTime;
            response.Address = address;

            return response;
        }

        #endregion

        #region 取消挂号
        /// <summary>
        /// 取消挂号
        /// </summary>
        [HttpPost]
        [ActionName("CancelOrder")]
        public CancelOrderResponse CancelOrder(CancelOrderRequest request)
        {
            //Logger.ErrorFormat("调用 BKCloud_CancelReg 失败，信息：{0}", JsonConvert.SerializeObject(request));
            OracleParameter[] parameter =
            {
                new OracleParameter("flowno", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
            };
            parameter[0].Value = request.FlowNo;
            parameter[1].Direction = System.Data.ParameterDirection.Output;
            parameter[2].Direction = System.Data.ParameterDirection.Output;

            try
            {
                OracleDataAccessHelper.RunProcedure("BKCloud_CancelOrder", parameter, "ds");
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_CancelReg 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new CancelOrderResponse();
            var resultCode = parameter[1].Value.ToString();
            var resultDesc = parameter[2].Value.ToString();

            HospSpLogHelper.Log("CancelOrder",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;

            return response;
        }

        #endregion

        #region 订单查询
        /// <summary>
        /// 订单查询
        /// </summary>
        [HttpPost]
        [ActionName("GetReservations")]
        public GetReservationsResponse GetReservations(GetReservationsRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("cardType", OracleDbType.Varchar2, 100),
                new OracleParameter("cardNo", OracleDbType.Varchar2, 100),
                new OracleParameter("flag", OracleDbType.Varchar2, 100),
                new OracleParameter("beginDate", OracleDbType.Varchar2, 100),
                new OracleParameter("endDate", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor)
            };
            parameter[0].Value = GetCardTypeName(request.CardType);
            parameter[1].Value = request.CardNo;
            parameter[2].Value = request.Flag;
            parameter[3].Value = request.BeginDate;
            parameter[4].Value = request.EndDate;
            parameter[5].Direction = System.Data.ParameterDirection.Output;
            parameter[6].Direction = System.Data.ParameterDirection.Output;
            parameter[7].Direction = System.Data.ParameterDirection.Output;
            DataTable dt;
            try
            {
                dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetReservations", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetReservations 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetReservationsResponse();
            var resultCode = parameter[5].Value.ToString();
            var resultDesc = parameter[6].Value.ToString();

            HospSpLogHelper.Log("GetReservations",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.ReservationList = new List<GetReservationsResponse.Reservation>();
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var flowNo = dt.Rows[i]["flowno"].ToString();
                var orderId = dt.Rows[i]["orderId"].ToString();
                var deptId = dt.Rows[i]["deptId"].ToString();
                var deptName = dt.Rows[i]["deptName"].ToString();
                //var doctorId = dt.Rows[i]["doctorId"].ToString();
                var doctorName = dt.Rows[i]["doctorName"].ToString();
                var regType = dt.Rows[i]["regType"].ToString();
                //var subjectName = dt.Rows[i]["subjectName"].ToString();
                var regNo = dt.Rows[i]["regNo"].ToString();
                var regDate = dt.Rows[i]["regDate"].ToString();
                var timeSlice = dt.Rows[i]["timeSlice"].ToString();
                var startTime = dt.Rows[i]["startTime"].ToString();
                var endTime = dt.Rows[i]["endTime"].ToString();
                var fee = dt.Rows[i]["fee"].ToString();
                var treatFee = dt.Rows[i]["treatFee"].ToString();
                var otherFee = dt.Rows[i]["otherFee"].ToString();
                var orderType = dt.Rows[i]["orderType"].ToString();

                var reservationObj = new GetReservationsResponse.Reservation();
                reservationObj.FlowNo = flowNo;
                reservationObj.OrderId = orderId;
                reservationObj.DeptId = deptId;
                reservationObj.DeptName = deptName;
                reservationObj.DoctorName = doctorName;
                reservationObj.RegType = regType;
                // reservationObj.SubjectName = subjectName;
                reservationObj.RegNo = regNo;
                reservationObj.RegDate = regDate;
                reservationObj.TimeSlice = timeSlice;
                reservationObj.StartTime = startTime;
                reservationObj.EndTime = endTime;
                reservationObj.Fee = fee;
                reservationObj.TreatFee = treatFee;
                reservationObj.OtherFee = otherFee;
                reservationObj.OrderType = orderType;

                response.ReservationList.Add(reservationObj);
            }

            return response;
        }
        #endregion

        #region 获取缴费单列表
        /// <summary>
        /// 获取缴费单列表
        /// </summary>
        [HttpPost]
        [ActionName("GetPrescriptions")]
        public GetPrescriptionListResponse GetPrescriptions(GetPrescriptionListRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("cardNo", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor),
            };
            parameter[0].Value = request.CardNo;
            parameter[1].Direction = System.Data.ParameterDirection.Output;
            parameter[2].Direction = System.Data.ParameterDirection.Output;
            parameter[3].Direction = System.Data.ParameterDirection.Output;

            DataTable dt;
            try
            {
                dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetPrescriptions", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetPrescriptions 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetPrescriptionListResponse();
            var resultCode = parameter[1].Value.ToString();
            var resultDesc = parameter[2].Value.ToString();

            HospSpLogHelper.Log("GetPrescriptions",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.PrescList = new List<GetPrescriptionListResponse.PrescInfo>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var deptObj = new GetPrescriptionListResponse.PrescInfo();
                var appointNo = dt.Rows[i]["appointNo"].ToString();
                var prescNo = dt.Rows[i]["prescNo"].ToString();
                var comDept = dt.Rows[i]["comDept"].ToString();
                var comDeptNo = dt.Rows[i]["comDeptNo"].ToString();
                var prescDept = dt.Rows[i]["prescDept"].ToString();
                var prescDeptNo = dt.Rows[i]["prescDeptNo"].ToString();
                var totalMoney = dt.Rows[i]["totalMoney"].ToString();
                var feeTypeName = dt.Rows[i]["feeTypeName"].ToString();
                var payMoney = dt.Rows[i]["payMoney"].ToString();
                var prescDate = dt.Rows[i]["prescDate"].ToString();
                var isPayed = dt.Rows[i]["isPayed"].ToString();

                deptObj.AppointNo = appointNo;
                deptObj.PrescNo = prescNo;
                deptObj.ComDept = comDept;
                deptObj.ComDeptNo = comDeptNo;
                deptObj.PrescDept = prescDept;
                deptObj.PrescDeptNo = prescDeptNo;
                deptObj.TotalMoney = totalMoney;
                deptObj.FeeTypeName = feeTypeName;
                deptObj.PayMoney = payMoney;
                deptObj.PrescDate = prescDate;
                deptObj.IsPayed = isPayed;

                response.PrescList.Add(deptObj);
            }
            return response;
        }

        #endregion

        #region 获取缴费单详情
        /// <summary>
        /// 获取缴费单详情
        /// </summary>
        [HttpPost]
        [ActionName("GetPrescriptionDetail")]
        public GetPrescriptionDetailResponse GetPrescriptionDetail(GetPrescriptionDetailRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("cardNo", OracleDbType.Varchar2, 100),
                new OracleParameter("prescNo", OracleDbType.Varchar2, 100),
                //new OracleParameter("comDeptNo", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor),
            };
            parameter[0].Value = request.CardNo;
            parameter[1].Value = request.PrescNo;
            //parameter[2].Value = request.ComDeptNo;
            parameter[2].Direction = System.Data.ParameterDirection.Output;
            parameter[3].Direction = System.Data.ParameterDirection.Output;
            parameter[4].Direction = System.Data.ParameterDirection.Output;

            DataTable dt;
            try
            {
                dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetPrescriptionDetail", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetPrescriptionDetail 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetPrescriptionDetailResponse();
            var resultCode = parameter[2].Value.ToString();
            var resultDesc = parameter[3].Value.ToString();

            HospSpLogHelper.Log("GetPrescriptionDetail",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.PrescItemList = new List<GetPrescriptionDetailResponse.PrescItemInfo>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var deptObj = new GetPrescriptionDetailResponse.PrescItemInfo();
                var name = dt.Rows[i]["itemName"].ToString();
                //var account = dt.Rows[i]["account"].ToString();
                //var itemPrice = dt.Rows[i]["itemPrice"].ToString();
                //var unit = dt.Rows[i]["unit"].ToString();
                var money = dt.Rows[i]["money"].ToString();
                var type = dt.Rows[i]["type"].ToString();

                deptObj.ItemName = name;
                //deptObj.Account = account;
                //deptObj.ItemPrice = itemPrice;
                //deptObj.Unit = unit;
                deptObj.Money = money;
                deptObj.Type = type;

                response.PrescItemList.Add(deptObj);
            }
            return response;
        }

        #endregion

        #region 查询缴费状态
        /// <summary>
        /// 查询缴费状态
        /// </summary>
        [HttpPost]
        [ActionName("GetPayStatus")]
        public GetPayStatusResponse GetPayStatus(GetPayStatusRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("cardNo", OracleDbType.Varchar2, 100),
                new OracleParameter("prescNo", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("status", OracleDbType.Varchar2, 100),
            };
            parameter[0].Value = request.CardNo;
            parameter[1].Value = request.ClinicNo;
            parameter[2].Direction = System.Data.ParameterDirection.Output;
            parameter[3].Direction = System.Data.ParameterDirection.Output;
            parameter[4].Direction = System.Data.ParameterDirection.Output;

            DataTable dt;
            try
            {
                OracleDataAccessHelper.RunProcedure("BKCloud_GetPayStatus", parameter, "ds");
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetPayStatus 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetPayStatusResponse();
            var resultCode = parameter[2].Value.ToString();
            var resultDesc = parameter[3].Value.ToString();
            var status = parameter[4].Value.ToString();

            HospSpLogHelper.Log("GetPayStatus",
            JsonConvert.SerializeObject(request),
            JsonConvert.SerializeObject(new
            {
                ResultCode = resultCode,
                ResultDesc = resultDesc,
                Status = status
            }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.Status = status;

            return response;
        }

        #endregion

        #region 缴费
        /// <summary>
        /// 预约挂号
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("ExePrescriptionPay")]
        public ExePrescriptionPayResponse ExePrescriptionPay(ExePrescriptionPayRequest request)
        {

            OracleParameter[] parameter =
            {
                new OracleParameter("cardNo", OracleDbType.Varchar2, 100),
                new OracleParameter("prescNo", OracleDbType.Varchar2, 100),
                new OracleParameter("payWay", OracleDbType.Varchar2, 100),
                new OracleParameter("totalFee", OracleDbType.Varchar2, 100),
                new OracleParameter("transNo", OracleDbType.Varchar2, 100),
                new OracleParameter("transTime", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("flowno", OracleDbType.Varchar2, 100),
                new OracleParameter("hisDate", OracleDbType.Varchar2, 100),
                new OracleParameter("pharmacyPosition", OracleDbType.Varchar2, 100),
                new OracleParameter("examineRoomPosition", OracleDbType.Varchar2, 100),
            };
            parameter[0].Value = request.CardNo;
            parameter[1].Value = request.PrescNo;
            parameter[2].Value = request.PayWay;
            parameter[3].Value = request.SumFee;
            parameter[4].Value = request.TransNo;
            parameter[5].Value = request.TransTime;
            parameter[6].Direction = System.Data.ParameterDirection.Output;
            parameter[7].Direction = System.Data.ParameterDirection.Output;
            parameter[8].Direction = System.Data.ParameterDirection.Output;
            parameter[9].Direction = System.Data.ParameterDirection.Output;
            parameter[10].Direction = System.Data.ParameterDirection.Output;
            parameter[11].Direction = System.Data.ParameterDirection.Output;

            try
            {
                OracleDataAccessHelper.RunProcedure("BKCloud_ExePrescriptionPay", parameter, "ds");
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_ExePrescriptionPay 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new ExePrescriptionPayResponse();
            var resultCode = parameter[6].Value.ToString();
            var resultDesc = parameter[7].Value.ToString();
            var flowNo = parameter[8].Value.ToString();
            var hisDate = parameter[9].Value.ToString();
            var pharmacyPosition = parameter[10].Value.ToString();
            var examineRoomPosition = parameter[11].Value.ToString();

            HospSpLogHelper.Log("ExePrescriptionPay",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    FlowNo = flowNo,
                    HisDate = hisDate,
                    PharmacyPosition = pharmacyPosition,
                    ExamineRoomPosition = examineRoomPosition
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.PharmacyPosition = pharmacyPosition;
            response.ExamineRoomPosition = examineRoomPosition;
            response.RcptNo = flowNo;
            response.HisDate = hisDate;

            return response;
        }

        #endregion

        #region 获取检验信息
        /// <summary>
        /// 检验单信息列表
        /// </summary>
        [HttpPost]
        [ActionName("GetExamInfo")]
        public GetExamInfoResponse GetExamInfo(GetExamInfoRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("cardNo", OracleDbType.Varchar2, 100),
                new OracleParameter("cardType", OracleDbType.Varchar2, 100),
                new OracleParameter("itemType", OracleDbType.Varchar2, 100),
                new OracleParameter("beginDate", OracleDbType.Varchar2, 100),
                new OracleParameter("endDate", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor),
            };
            parameter[0].Value = request.CardNo;
            parameter[1].Value = request.CardType;
            parameter[2].Value = request.ItemType;
            parameter[3].Value = request.BeginDate;
            parameter[4].Value = request.EndDate;
            parameter[5].Direction = System.Data.ParameterDirection.Output;
            parameter[6].Direction = System.Data.ParameterDirection.Output;
            parameter[7].Direction = System.Data.ParameterDirection.Output;

            DataTable dt;
            try
            {
                dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetExamInfo", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetExamInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetExamInfoResponse();
            var resultCode = parameter[5].Value.ToString();
            var resultDesc = parameter[6].Value.ToString();

            HospSpLogHelper.Log("GetExamInfo",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.ExamList = new List<GetExamInfoResponse.ExamInfo>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var deptObj = new GetExamInfoResponse.ExamInfo();
                var examId = dt.Rows[i]["examId"].ToString();
                var examName = dt.Rows[i]["examName"].ToString();
                var examDate = dt.Rows[i]["examDate"].ToString();
                var patientId = dt.Rows[i]["patientId"].ToString();
                var patientName = dt.Rows[i]["patientName"].ToString();
                var patientSex = dt.Rows[i]["patientSex"].ToString();
                var status = dt.Rows[i]["status"].ToString();

                deptObj.ExamID = examId;
                deptObj.ExamName = examName;
                deptObj.ExamDate = examDate;
                deptObj.CardNo = patientId;
                deptObj.PatientID = patientId;
                deptObj.PatientName = patientName;
                deptObj.PatientSex = patientSex;
                deptObj.Status = status;

                response.ExamList.Add(deptObj);
            }
            return response;
        }


        /// <summary>
        /// 检验单信息详情
        /// </summary>
        [HttpPost]
        [ActionName("GetExamDetail")]
        public GetExamDetailResponse GetExamDetail(GetExamDetailRequest request)
        {
            Logger.ErrorFormat("调用 GetExamDetail 失败，信息：{0}", JsonConvert.SerializeObject(request));
            OracleParameter[] parameter =
            {
                new OracleParameter("cardNo", OracleDbType.Varchar2, 100),
                new OracleParameter("examId", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor),
            };
            parameter[0].Value = request.CardNo;
            parameter[1].Value = request.ExamId;
            parameter[2].Direction = System.Data.ParameterDirection.Output;
            parameter[3].Direction = System.Data.ParameterDirection.Output;
            parameter[4].Direction = System.Data.ParameterDirection.Output;

            DataTable dt;
            try
            {
                dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetExamDetail", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetExamDetail 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetExamDetailResponse();
            var resultCode = parameter[2].Value.ToString();
            var resultDesc = parameter[3].Value.ToString();

            HospSpLogHelper.Log("GetExamDetail",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.ExamItemList = new List<GetExamDetailResponse.ExamItemInfo>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var deptObj = new GetExamDetailResponse.ExamItemInfo();
                var itemNo = dt.Rows[i]["itemNo"].ToString();
                var itemDate = dt.Rows[i]["itemDate"].ToString();
                var itemName = dt.Rows[i]["itemName"].ToString();
                var labRepItemNo = dt.Rows[i]["labRepItemNo"].ToString();
                var labRepItemName = dt.Rows[i]["labRepItemName"].ToString();
                var labRepResult = dt.Rows[i]["labRepResult"].ToString();
                var labRepUnits = dt.Rows[i]["labRepUnits"].ToString();
                var labInd = dt.Rows[i]["labInd"].ToString();
                var labContext = dt.Rows[i]["labContext"].ToString();

                deptObj.ItemNo = itemNo;
                deptObj.ItemDate = itemDate;
                deptObj.LabItemName = itemName;
                deptObj.LabRepItemNo = labRepItemNo;
                deptObj.LabRepItemName = labRepItemName;
                deptObj.LabRepResult = labRepResult;
                deptObj.LabRepUnits = labRepUnits;
                deptObj.LabInd = labInd;
                deptObj.LabContext = labContext;

                response.ExamItemList.Add(deptObj);
            }
            return response;
        }
        #endregion

        #region 获取检查信息
        /// <summary>
        /// 检验单信息列表
        /// </summary>
        [HttpPost]
        [ActionName("GetCheckReportInfo")]
        public GetExamInfoResponse GetCheckReportInfo(GetExamInfoRequest request)
        {
            OracleParameter[] parameter =
            {
                new OracleParameter("cardNo", OracleDbType.Varchar2, 100),
                new OracleParameter("cardType", OracleDbType.Varchar2, 100),
                new OracleParameter("itemType", OracleDbType.Varchar2, 100),
                new OracleParameter("beginDate", OracleDbType.Varchar2, 100),
                new OracleParameter("endDate", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor),
            };
            parameter[0].Value = request.CardNo;
            parameter[1].Value = request.CardType;
            parameter[2].Value = request.ItemType;
            parameter[3].Value = request.BeginDate;
            parameter[4].Value = request.EndDate;
            parameter[5].Direction = System.Data.ParameterDirection.Output;
            parameter[6].Direction = System.Data.ParameterDirection.Output;
            parameter[7].Direction = System.Data.ParameterDirection.Output;

            DataTable dt;
            try
            {
                dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetCheckReportInfo", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetCheckReportInfo 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetExamInfoResponse();
            var resultCode = parameter[5].Value.ToString();
            var resultDesc = parameter[6].Value.ToString();

            HospSpLogHelper.Log("GetCheckReportInfo",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.ExamList = new List<GetExamInfoResponse.ExamInfo>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var deptObj = new GetExamInfoResponse.ExamInfo();
                var examId = dt.Rows[i]["examId"].ToString();
                var examName = dt.Rows[i]["examName"].ToString();
                var examDate = dt.Rows[i]["examDate"].ToString();
                var patientId = dt.Rows[i]["patientId"].ToString();
                var patientName = dt.Rows[i]["patientName"].ToString();
                var patientSex = dt.Rows[i]["patientSex"].ToString();
                var status = dt.Rows[i]["status"].ToString();

                deptObj.ExamID = examId;
                deptObj.ExamName = examName;
                deptObj.ExamDate = examDate;
                deptObj.CardNo = patientId;
                deptObj.PatientID = patientId;
                deptObj.PatientName = patientName;
                deptObj.PatientSex = patientSex;
                deptObj.Status = status;

                response.ExamList.Add(deptObj);
            }
            return response;
        }


        /// <summary>
        /// 检验单信息详情
        /// </summary>
        [HttpPost]
        [ActionName("GetCheckReportInfo")]
        public GetCheckReportDetailResponse GetCheckReportInfo(GetCheckReportDetailRequest request)
        {
            Logger.ErrorFormat("调用 GetExamDetail 失败，信息：{0}", JsonConvert.SerializeObject(request));
            OracleParameter[] parameter =
            {
                new OracleParameter("cardNo", OracleDbType.Varchar2, 100),
                new OracleParameter("examId", OracleDbType.Varchar2, 100),
                new OracleParameter("resultCode", OracleDbType.Varchar2, 100),
                new OracleParameter("resultDesc", OracleDbType.Varchar2, 100),
                new OracleParameter("cx_cursor", OracleDbType.RefCursor),
            };
            parameter[0].Value = request.CardNo;
            parameter[1].Value = request.ExamId;
            parameter[2].Direction = System.Data.ParameterDirection.Output;
            parameter[3].Direction = System.Data.ParameterDirection.Output;
            parameter[4].Direction = System.Data.ParameterDirection.Output;

            DataTable dt;
            try
            {
                dt = OracleDataAccessHelper.RunProcedure("BKCloud_GetCheckReportDetail", parameter, "ds").Tables[0];
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用 BKCloud_GetCheckReportDetail 失败，信息：{0}", JsonConvert.SerializeObject(ex));

                throw;
            }

            var response = new GetCheckReportDetailResponse();
            var resultCode = parameter[2].Value.ToString();
            var resultDesc = parameter[3].Value.ToString();

            HospSpLogHelper.Log("GetCheckReportDetail",
                JsonConvert.SerializeObject(request),
                JsonConvert.SerializeObject(new
                {
                    ResultCode = resultCode,
                    ResultDesc = resultDesc,
                    Data = dt
                }));

            if (resultCode != "0")
            {
                response.ResultCode = resultCode;
                response.ResultDesc = resultDesc;
                return response;
            }

            response.ResultCode = resultCode;
            response.ResultDesc = resultDesc;
            response.CheckItemList = new List<GetCheckReportDetailResponse.CheckItemInfo>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var deptObj = new GetCheckReportDetailResponse.CheckItemInfo();
                var examId = dt.Rows[i]["examId"].ToString();
                var prompt = dt.Rows[i]["prompt"].ToString();
                var content = dt.Rows[i]["content"].ToString();

                deptObj.ExamId = examId;
                deptObj.Prompt = prompt;
                deptObj.Content = content;

                response.CheckItemList.Add(deptObj);
            }
            return response;
        }
        #endregion


        private string GetCardTypeName(string cardType)
        {
            if (cardType == "0")
            {
                return "就诊卡";
            }

            return "";
        }
    }
}
